{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T10:53:25.695081Z",
     "start_time": "2025-01-06T10:53:06.503887Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#Numpy的数组和原生python的性能对比\n",
    "import random\n",
    "import time\n",
    "import numpy as np\n",
    "\n",
    "a = []\n",
    "for i in range(100000000):\n",
    "    a.append(random.random())\n",
    "t1 = time.time()\n",
    "sum1 = sum(a)\n",
    "t2 = time.time()\n",
    "b = np.array(a)\n",
    "t4 = time.time()\n",
    "sum3 = np.sum(b)\n",
    "t5 = time.time()\n",
    "print(t2 - t1, t5 - t4)"
   ],
   "id": "1f350a640439046c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.6250419616699219 0.12500905990600586\n"
     ]
    }
   ],
   "execution_count": 36
  },
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-01-06T06:03:23.713084Z",
     "start_time": "2025-01-06T06:03:23.439030Z"
    }
   },
   "source": [
    "list1 = [1, 2, 3, 4]\n",
    "oneArray = np.array(list1)\n",
    "print(type(oneArray))\n",
    "print(oneArray)"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n",
      "[1 2 3 4]\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T06:04:21.040780Z",
     "start_time": "2025-01-06T06:04:21.032309Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 创建数组的多种形式\n",
    "# 1. 直接传入列表的方式\n",
    "t1 = np.array([1, 2, 3])\n",
    "print(t1)\n",
    "print(type(t1))"
   ],
   "id": "833a0678b54afd22",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3]\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T06:04:55.302670Z",
     "start_time": "2025-01-06T06:04:55.292922Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 2. 传入range生成序列\n",
    "t2 = np.array(range(10))\n",
    "print(t2)\n",
    "print(type(t2))"
   ],
   "id": "3793f7450b664b3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5 6 7 8 9]\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T06:05:07.412689Z",
     "start_time": "2025-01-06T06:05:07.406530Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 3. 使用numpy自带的np.arange()生成数组\n",
    "t3 = np.arange(0, 10, 2)\n",
    "print(t3)\n",
    "print(type(t3))"
   ],
   "id": "d0121d5240102fa1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 2 4 6 8]\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T06:05:54.178578Z",
     "start_time": "2025-01-06T06:05:54.173357Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#创建二维数组\n",
    "list2 = [[1, 2], [3, 4], [5, 6]]\n",
    "twoArray = np.array(list2)\n",
    "print(twoArray)"
   ],
   "id": "29a1315e2ff6cb11",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T06:06:23.448890Z",
     "start_time": "2025-01-06T06:06:23.436852Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#常用属性\n",
    "list2 = [[1, 2], [3, 4], [5, 6]]\n",
    "twoArray = np.array(list2)\n",
    "# 获取数组的维度( 注意： 与函数的参数很像)\n",
    "print(twoArray.ndim)\n",
    "# 形状（行，列）\n",
    "print(twoArray.shape)\n",
    "# 有多少个元素\n",
    "print(twoArray.size)"
   ],
   "id": "7c76dddeecf821e7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "(3, 2)\n",
      "6\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T06:12:15.216543Z",
     "start_time": "2025-01-06T06:12:15.207069Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#调整数组形状\n",
    "four = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "# 修改的是原有的\n",
    "four.shape = (3, 2)\n",
    "print(four)\n",
    "# 返回一个新的数组\n",
    "four = four.reshape(3, 2)\n",
    "print(four)\n",
    "# 将多维变成一维数组\n",
    "five = four.reshape((6,), order='F')\n",
    "# 默认情况下‘C’以行为主的顺序展开，‘F’（Fortran风格）意味着以列的顺序展开\n",
    "six = four.flatten(order='F')\n",
    "print(five)\n",
    "print(six)\n",
    "# 拓展：数组的形状\n",
    "t = np.arange(24)\n",
    "print(t)\n",
    "print(t.shape)\n",
    "# 转换成二维\n",
    "t1 = t.reshape((4, 6))\n",
    "print(t1)\n",
    "print(t1.shape)\n",
    "#转化成三维\n",
    "t2 = t.reshape((2, 3, 4))\n",
    "print(t2)\n",
    "print(t2.shape)"
   ],
   "id": "395e12adbd745ceb",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "[1 3 5 2 4 6]\n",
      "[1 3 5 2 4 6]\n",
      "[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]\n",
      "(24,)\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "(4, 6)\n",
      "[[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[12 13 14 15]\n",
      "  [16 17 18 19]\n",
      "  [20 21 22 23]]]\n",
      "(2, 3, 4)\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T06:13:43.993101Z",
     "start_time": "2025-01-06T06:13:43.987969Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#将数组转化成列表\n",
    "a = np.array([9, 12, 88, 14, 25])\n",
    "list_a = a.tolist()\n",
    "print(list_a)\n",
    "print(type(list_a))"
   ],
   "id": "638e5cc949c961d4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[9, 12, 88, 14, 25]\n",
      "<class 'list'>\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T06:23:01.624953Z",
     "start_time": "2025-01-06T06:23:01.616911Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#numpy的数据类型\n",
    "import random\n",
    "\n",
    "# 返回数组中每个元素的字节单位长度，dtype设置数据类型\n",
    "f = np.array([1, 2, 3, 4, 5], dtype=np.int16)\n",
    "print(f.itemsize)  # 1 np.int8(一个字节)\n",
    "# 获取数据类型\n",
    "print(f.dtype)"
   ],
   "id": "19ad29754b1fe21d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "int16\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T06:23:17.615341Z",
     "start_time": "2025-01-06T06:23:17.604478Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 调整数据类型\n",
    "f1 = f.astype(np.int64)\n",
    "print(f1.dtype)"
   ],
   "id": "4a48597c2270860d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "int64\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T06:24:57.718351Z",
     "start_time": "2025-01-06T06:24:57.710277Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 拓展随机生成小数\n",
    "# 使用python语法，保留两位\n",
    "print(round(random.random(), 2))\n",
    "arr = np.array([random.random() for i in range(10)])\n",
    "print(arr)\n",
    "# 取小数点后两位\n",
    "print(np.round(arr, 2))"
   ],
   "id": "1dcaffe1e11f69e9",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.64\n",
      "[0.28120228 0.05949423 0.53515872 0.0698872  0.53440983 0.88217994\n",
      " 0.36001224 0.91860758 0.05105855 0.54229633]\n",
      "[0.28 0.06 0.54 0.07 0.53 0.88 0.36 0.92 0.05 0.54]\n"
     ]
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T06:33:54.379015Z",
     "start_time": "2025-01-06T06:33:54.362875Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#6.数组的计算\n",
    "#6.1数组和数的计算\n",
    "t1 = np.arange(24).reshape((6, 4))\n",
    "print(t1)\n",
    "print(t1 + 2)\n",
    "print(\"-\" * 20)\n",
    "print(t1 * 2)\n",
    "print(\"-\" * 20)\n",
    "print(t1 / 2)"
   ],
   "id": "335470046a794020",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [12 13 14 15]\n",
      " [16 17 18 19]\n",
      " [20 21 22 23]]\n",
      "[[ 2  3  4  5]\n",
      " [ 6  7  8  9]\n",
      " [10 11 12 13]\n",
      " [14 15 16 17]\n",
      " [18 19 20 21]\n",
      " [22 23 24 25]]\n",
      "--------------------\n",
      "[[ 0  2  4  6]\n",
      " [ 8 10 12 14]\n",
      " [16 18 20 22]\n",
      " [24 26 28 30]\n",
      " [32 34 36 38]\n",
      " [40 42 44 46]]\n",
      "--------------------\n",
      "[[ 0.   0.5  1.   1.5]\n",
      " [ 2.   2.5  3.   3.5]\n",
      " [ 4.   4.5  5.   5.5]\n",
      " [ 6.   6.5  7.   7.5]\n",
      " [ 8.   8.5  9.   9.5]\n",
      " [10.  10.5 11.  11.5]]\n"
     ]
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T06:34:55.759591Z",
     "start_time": "2025-01-06T06:34:55.740773Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#6.2数组与数组之间的操作\n",
    "t1 = np.arange(24).reshape((6, 4))\n",
    "t2 = np.arange(100, 124).reshape((6, 4))\n",
    "print(t1 + t2)\n",
    "print(t1 * t2)"
   ],
   "id": "36f7a8f7720c6ed2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[100 102 104 106]\n",
      " [108 110 112 114]\n",
      " [116 118 120 122]\n",
      " [124 126 128 130]\n",
      " [132 134 136 138]\n",
      " [140 142 144 146]]\n",
      "[[   0  101  204  309]\n",
      " [ 416  525  636  749]\n",
      " [ 864  981 1100 1221]\n",
      " [1344 1469 1596 1725]\n",
      " [1856 1989 2124 2261]\n",
      " [2400 2541 2684 2829]]\n"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T06:49:16.568007Z",
     "start_time": "2025-01-06T06:49:16.560010Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#6.3数组中的轴\n",
    "a = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "print(np.sum(a, axis=0))\n",
    "print(np.sum(a, axis=1))\n",
    "print(np.sum(a))"
   ],
   "id": "3e8a3c3542f7096a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5 7 9]\n",
      "[ 6 15]\n",
      "21\n"
     ]
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T06:51:48.418647Z",
     "start_time": "2025-01-06T06:51:48.400772Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.arange(24).reshape((2, 3, 4))\n",
    "print(a)"
   ],
   "id": "50eeb8a18add179b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[12 13 14 15]\n",
      "  [16 17 18 19]\n",
      "  [20 21 22 23]]]\n"
     ]
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T06:51:50.905312Z",
     "start_time": "2025-01-06T06:51:50.895515Z"
    }
   },
   "cell_type": "code",
   "source": [
    "b = np.sum(a, axis=0)\n",
    "print(\"-\" * 20)\n",
    "print(b)\n",
    "c = np.sum(a, axis=1)\n",
    "print(\"-\" * 20)\n",
    "print(c)\n",
    "d = np.sum(a, axis=2)\n",
    "print(\"-\" * 20)\n",
    "print(d)"
   ],
   "id": "7999fff5a1a0e9af",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------\n",
      "[[12 14 16 18]\n",
      " [20 22 24 26]\n",
      " [28 30 32 34]]\n",
      "--------------------\n",
      "[[12 15 18 21]\n",
      " [48 51 54 57]]\n",
      "--------------------\n",
      "[[ 6 22 38]\n",
      " [54 70 86]]\n"
     ]
    }
   ],
   "execution_count": 24
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T06:54:06.288703Z",
     "start_time": "2025-01-06T06:54:06.282697Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#7.数组的索引和切片\n",
    "#一维数组\n",
    "a = np.arange(10)\n",
    "# 冒号分隔切片参数 start:stop:step 来进行切片操作 print(a[2:7:2])# 从索引 2 开始到索引 7 停止，间隔为 2\n",
    "# 如果只放置一个参数， 如 [2]， 将返回与该索引相对应的单个元素\n",
    "print(a[2], a)\n",
    "# 如果为 [2:]， 表示从该索引开始以后的所有项都将被提取\n",
    "print(a[2:])"
   ],
   "id": "4ce11e41dd96e1f3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 [0 1 2 3 4 5 6 7 8 9]\n",
      "[2 3 4 5 6 7 8 9]\n"
     ]
    }
   ],
   "execution_count": 25
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T06:54:51.625401Z",
     "start_time": "2025-01-06T06:54:51.611475Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#多维数组\n",
    "t1 = np.arange(24).reshape(4, 6)\n",
    "print(t1)\n",
    "print('*' * 20)\n",
    "print(t1[1])  # 取一行(一行代表是一条数据， 索引也是从0开始的) print(t1[1,:]) # 取一行\n",
    "print('*' * 20)\n",
    "print(t1[1:])  # 取连续的多行\n",
    "print('*' * 20)\n",
    "print(t1[1:3, :])  # 取连续的多行\n",
    "print('*' * 20)\n",
    "print(t1[[0, 2, 3]])  # 取不连续的多行\n",
    "print('*' * 20)\n",
    "print(t1[[0, 2, 3], :])  # 取不连续的多行\n",
    "print('*' * 20)\n",
    "print(t1[:, 1])  # 取一列\n",
    "print('*' * 20)\n",
    "print(t1[:, 1:])  # 连续的多列\n",
    "print('*' * 20)\n",
    "print(t1[:, [0, 2, 3]])  # 取不连续的多列\n",
    "print('*' * 20)\n",
    "print(t1[2, 3])  # # 取某一个值,三行四列\n",
    "print('*' * 20)"
   ],
   "id": "621f0d3f77966798",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "********************\n",
      "[ 6  7  8  9 10 11]\n",
      "********************\n",
      "[[ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "********************\n",
      "[[ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]]\n",
      "********************\n",
      "[[ 0  1  2  3  4  5]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "********************\n",
      "[[ 0  1  2  3  4  5]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "********************\n",
      "[ 1  7 13 19]\n",
      "********************\n",
      "[[ 1  2  3  4  5]\n",
      " [ 7  8  9 10 11]\n",
      " [13 14 15 16 17]\n",
      " [19 20 21 22 23]]\n",
      "********************\n",
      "[[ 0  2  3]\n",
      " [ 6  8  9]\n",
      " [12 14 15]\n",
      " [18 20 21]]\n",
      "********************\n",
      "15\n",
      "********************\n"
     ]
    }
   ],
   "execution_count": 26
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T07:28:21.410658Z",
     "start_time": "2025-01-06T07:28:21.395860Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#8.数组中的数值修改\n",
    "t = np.arange(24).reshape(4, 6)\n",
    "# 修改某一行的值\n",
    "t[1, :] = 0\n",
    "# 修改某一列的值\n",
    "t[:, 1] = 0\n",
    "# 修改连续多行\n",
    "t[1:3, :] = 0\n",
    "# 修改连续多列\n",
    "t[:, 1:4] = 0\n",
    "# 修改多行多列， 取第二行到第四行， 第三列到第五列\n",
    "t[1:4, 2:5] = 0\n",
    "# 修改多个不相邻的点\n",
    "t[[0, 1], [0, 3]] = 0\n",
    "print(t)\n",
    "# 可以根据条件修改， 比如将小于 10 的值改掉\n",
    "t[t < 10] = 0\n",
    "print(t)\n",
    "# 使用逻辑判断\n",
    "# np.logical_and & # np.logical_or |\n",
    "# np.logical_not ~\n",
    "t[(t > 2) & (t < 6)] = 0  # 与\n",
    "t[(t < 2) | (t > 6)] = 0  # 或\n",
    "t[~(t > 6)] = 0  # 非\n",
    "print(t)\n",
    "t = t.clip(10, 18)\n",
    "print(t)"
   ],
   "id": "ca0924ea77e82189",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  0  0  0  4  5]\n",
      " [ 0  0  0  0  0  0]\n",
      " [ 0  0  0  0  0  0]\n",
      " [18  0  0  0  0 23]]\n",
      "[[ 0  0  0  0  0  0]\n",
      " [ 0  0  0  0  0  0]\n",
      " [ 0  0  0  0  0  0]\n",
      " [18  0  0  0  0 23]]\n",
      "[[0 0 0 0 0 0]\n",
      " [0 0 0 0 0 0]\n",
      " [0 0 0 0 0 0]\n",
      " [0 0 0 0 0 0]]\n",
      "[[10 10 10 10 10 10]\n",
      " [10 10 10 10 10 10]\n",
      " [10 10 10 10 10 10]\n",
      " [10 10 10 10 10 10]]\n"
     ]
    }
   ],
   "execution_count": 27
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T07:34:47.737134Z",
     "start_time": "2025-01-06T07:34:47.728699Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#9 数组的添加、删除和去重\n",
    "#9.1 数组的添加\n",
    "# 1. numpy.append 函数在数组的末尾添加值。 追加操作会分配整个数组， 并把原来的数组复制到新数组中。\n",
    "# 此外， 输入数组的维度必须匹配否则将生成 ValueError。\n",
    "'''\n",
    "参 数 说 明 ：\n",
    "arr： 输 入 数 组\n",
    "values： 要向 arr 添加的值， 需要和 arr 形状相同（除了要添加的轴）\n",
    "axis： 默认为 None。 当 axis 无定义时， 是横向加成， 返回总是为一维数组！ 当 axis 有定义的\n",
    "时候， 分别为 0 和 1 的时候。 当\n",
    "axis 有定义的时候， 分别为 0 和 1 的时候（列数要相同） 。 当 axis 为 1 时， 数组是加在右边\n",
    "（行数要相同） 。\n",
    "'''\n",
    "import numpy as np\n",
    "\n",
    "a = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "print('第一个数组： ')\n",
    "print(a)\n",
    "print('\\n')\n",
    "print(' 向 数 组 添 加 元 素 ： ')\n",
    "print(np.append(a, [7, 8, 9]))\n",
    "print('\\n')\n",
    "print('沿轴 0 添加元素： ')\n",
    "print(np.append(a, [[7, 8, 9]], axis=0))\n",
    "print('\\n')\n",
    "print('沿轴 1 添加元素： ')\n",
    "print(np.append(a, [[5, 5, 5], [7, 8, 9]], axis=1))\n",
    "# 2. numpy.insert\n",
    "# 函数在给定索引之前， 沿给定轴在输入数组中插入值。 # 如果值的类型转换为要插入， 则它与输入数组不同。\n",
    "# 插入没有原地的， 函数会返回一个新数组。 此外， 如果未提供轴， 则输入数组会被展开。\n",
    "a = np.array([[1, 2], [3, 4], [5, 6]])\n",
    "print('第一个数组： ')\n",
    "print(a)\n",
    "print('\\n')\n",
    "print('未传递 Axis 参数。 在插入之前输入数组会被展开。 ')\n",
    "print(np.insert(a, 3, [11, 12]))\n",
    "print('\\n')\n",
    "print('传递了 Axis 参数。 会广播值数组来配输入数组。 ')\n",
    "print('沿轴 0 广播： ')\n",
    "print(np.insert(a, 1, [11], axis=0))\n",
    "print('\\n')\n",
    "print('沿轴 1 广播： ')\n",
    "print(np.insert(a, 1, 11, axis=1))"
   ],
   "id": "36d73b2d357a91c1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组： \n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "\n",
      "\n",
      " 向 数 组 添 加 元 素 ： \n",
      "[1 2 3 4 5 6 7 8 9]\n",
      "\n",
      "\n",
      "沿轴 0 添加元素： \n",
      "[[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n",
      "\n",
      "\n",
      "沿轴 1 添加元素： \n",
      "[[1 2 3 5 5 5]\n",
      " [4 5 6 7 8 9]]\n",
      "第一个数组： \n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "\n",
      "\n",
      "未传递 Axis 参数。 在插入之前输入数组会被展开。 \n",
      "[ 1  2  3 11 12  4  5  6]\n",
      "\n",
      "\n",
      "传递了 Axis 参数。 会广播值数组来配输入数组。 \n",
      "沿轴 0 广播： \n",
      "[[ 1  2]\n",
      " [11 11]\n",
      " [ 3  4]\n",
      " [ 5  6]]\n",
      "\n",
      "\n",
      "沿轴 1 广播： \n",
      "[[ 1 11  2]\n",
      " [ 3 11  4]\n",
      " [ 5 11  6]]\n"
     ]
    }
   ],
   "execution_count": 29
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T07:40:01.943902Z",
     "start_time": "2025-01-06T07:40:01.937679Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#9.2 数组中的删除\n",
    "#numpy.delete 函数返回从输入数组中删除指定子数组的新数组。 与 insert() 函数的情况一样，\n",
    "# 如果未提供轴参数， 则输入数组将展开。\n",
    "'''\n",
    "参 数 说 明 ： arr： 输 入 数 组\n",
    "obj： 可以被切片， 整数或者整数数组， 表明要从输入数组删除的子数组 axis：\n",
    "沿着它删除给定子数组的轴， 如果未提供， 则输入数组会被展开\n",
    "'''\n",
    "a = np.arange(12).reshape(3, 4)\n",
    "print('第一个数组： ')\n",
    "print(a)\n",
    "print('\\n')\n",
    "print('未传递 Axis 参数。 在删除之前输入数组会被展开。 ')\n",
    "print(np.delete(a, 5))\n",
    "print('\\n')\n",
    "print('删除每一行中的第二列： ')\n",
    "print(np.delete(a, 1, axis=1))\n",
    "print('\\n')"
   ],
   "id": "6810696ec371be0e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组： \n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "\n",
      "\n",
      "未传递 Axis 参数。 在删除之前输入数组会被展开。 \n",
      "[ 0  1  2  3  4  6  7  8  9 10 11]\n",
      "\n",
      "\n",
      "删除每一行中的第二列： \n",
      "[[ 0  2  3]\n",
      " [ 4  6  7]\n",
      " [ 8 10 11]]\n",
      "\n",
      "\n"
     ]
    }
   ],
   "execution_count": 30
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T07:40:34.297223Z",
     "start_time": "2025-01-06T07:40:34.284630Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#9.3数组去重\n",
    "a = np.array([5, 2, 6, 2, 7, 5, 6, 8, 2, 9])\n",
    "print('第一个数组： ')\n",
    "print(a)\n",
    "print('\\n')\n",
    "print('第一个数组的去重值： ')\n",
    "u = np.unique(a)\n",
    "print(u)\n",
    "print('\\n')\n",
    "print('去重数组的索引数组： ')\n",
    "u, indices = np.unique(a, return_index=True)\n",
    "print(indices)\n",
    "print('\\n')\n",
    "print('我们可以看到每个和原数组下标对应的数值： ')\n",
    "print(a)\n",
    "print('去重数组的下标： ')\n",
    "u, indices = np.unique(a, return_inverse=True)\n",
    "print(u)\n",
    "print(indices)\n",
    "print('\\n')\n",
    "print('返回去重元素的重复数量： ')\n",
    "u, indices = np.unique(a, return_counts=True)\n",
    "print(u)\n",
    "print(indices)"
   ],
   "id": "aa66f8f5061e6ed5",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组： \n",
      "[5 2 6 2 7 5 6 8 2 9]\n",
      "\n",
      "\n",
      "第一个数组的去重值： \n",
      "[2 5 6 7 8 9]\n",
      "\n",
      "\n",
      "去重数组的索引数组： \n",
      "[1 0 2 4 7 9]\n",
      "\n",
      "\n",
      "我们可以看到每个和原数组下标对应的数值： \n",
      "[5 2 6 2 7 5 6 8 2 9]\n",
      "去重数组的下标： \n",
      "[2 5 6 7 8 9]\n",
      "[1 0 2 0 3 1 2 4 0 5]\n",
      "\n",
      "\n",
      "返回去重元素的重复数量： \n",
      "[2 5 6 7 8 9]\n",
      "[3 2 2 1 1 1]\n"
     ]
    }
   ],
   "execution_count": 31
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "#10numpy的计算（数学函数）",
   "id": "d3824b09ea60a46a"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T08:00:57.679917Z",
     "start_time": "2025-01-06T08:00:57.665303Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#11数组的拼接与分割\n",
    "#11.1数组拼接\n",
    "# 有的时候我们需要将两个数据加起来一起研究分析， 我们就可以将其进行拼接然后分析\n",
    "import numpy as np\n",
    "\n",
    "# 1. 根据轴连接的数组序列\n",
    "a = np.array([[1, 2], [3, 4]])\n",
    "b = np.array([[5, 6], [7, 8]])\n",
    "# 要求 a,b 两个数组的维度相同\n",
    "# print ('沿轴 0 连接两个数组： ')\n",
    "print(np.concatenate((a, b), axis=0))\n",
    "print('\\n')\n",
    "print('沿轴 1 连接两个数组： ')\n",
    "print(np.concatenate((a, b), axis=1))\n",
    "# 2. 根据轴进行堆叠\n",
    "print('沿轴 0 连接两个数组： ')\n",
    "print(np.stack((a, b), axis=0))\n",
    "print('\\n')\n",
    "print('沿轴 1 连接两个数组： ')\n",
    "print(np.stack((a, b), axis=1))\n",
    "# 3. 矩阵垂直拼接\n",
    "v1 = [[0, 1, 2, 3, 4, 5],\n",
    "      [6, 7, 8, 9, 10, 11]]\n",
    "v2 = [[12, 13, 14, 15, 16, 17],\n",
    "      [18, 19, 20, 21, 22, 23]]\n",
    "result = np.vstack((v1, v2))\n",
    "print(result)\n",
    "# 4. 矩阵水平拼接\n",
    "v1 = [[0, 1, 2, 3, 4, 5],\n",
    "      [6, 7, 8, 9, 10, 11]]\n",
    "v2 = [[12, 13, 14, 15, 16, 17],\n",
    "      [18, 19, 20, 21, 22, 23]]\n",
    "result = np.hstack((v1, v2))\n",
    "print(result)"
   ],
   "id": "817994ed17b43177",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]\n",
      " [7 8]]\n",
      "\n",
      "\n",
      "沿轴 1 连接两个数组： \n",
      "[[1 2 5 6]\n",
      " [3 4 7 8]]\n",
      "沿轴 0 连接两个数组： \n",
      "[[[1 2]\n",
      "  [3 4]]\n",
      "\n",
      " [[5 6]\n",
      "  [7 8]]]\n",
      "\n",
      "\n",
      "沿轴 1 连接两个数组： \n",
      "[[[1 2]\n",
      "  [5 6]]\n",
      "\n",
      " [[3 4]\n",
      "  [7 8]]]\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "[[ 0  1  2  3  4  5 12 13 14 15 16 17]\n",
      " [ 6  7  8  9 10 11 18 19 20 21 22 23]]\n"
     ]
    }
   ],
   "execution_count": 32
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T08:01:33.021092Z",
     "start_time": "2025-01-06T08:01:33.013372Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#11.2数组分割\n",
    "# 1. 将一个数组分割为多个子数组\n",
    "'''\n",
    "参数说明：\n",
    "ary： 被分割的数组\n",
    "indices_or_sections： 是一个整数， 就用该数平均切分， 如果是一个数组， 为沿轴切分的位置\n",
    "（左开右闭）\n",
    "axis： 沿着哪个维度进行切向， 默认为 0， 横向切分。 为 1 时， 纵向切分\n",
    "'''\n",
    "\n",
    "arr = np.arange(9).reshape(3, 3)\n",
    "print('将数组分为三个大小相等的子数组： ')\n",
    "b = np.split(arr, 3)\n",
    "print(b)\n",
    "# 2.numpy.hsplit 函数用于水平分割数组， 通过指定要返回的相同形状的数组数量来拆分原数组。\n",
    "harr = np.floor(10 * np.random.random((2, 6)))\n",
    "print('原 array： ')\n",
    "print(harr)\n",
    "print('拆分后： ')\n",
    "print(np.hsplit(harr, 3))\n",
    "# 3.numpy.vsplit 沿着垂直轴分割\n",
    "a = np.arange(16).reshape(4, 4)\n",
    "print('第一个数组： ')\n",
    "print(a)\n",
    "print('\\n')\n",
    "print('竖直分割： ')\n",
    "b = np.vsplit(a, 2)\n",
    "print(b)"
   ],
   "id": "c235c4a91487ba17",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "将数组分为三个大小相等的子数组： \n",
      "[array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7, 8]])]\n",
      "原 array： \n",
      "[[5. 8. 5. 5. 7. 7.]\n",
      " [5. 7. 7. 4. 5. 1.]]\n",
      "拆分后： \n",
      "[array([[5., 8.],\n",
      "       [5., 7.]]), array([[5., 5.],\n",
      "       [7., 4.]]), array([[7., 7.],\n",
      "       [5., 1.]])]\n",
      "第一个数组： \n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [12 13 14 15]]\n",
      "\n",
      "\n",
      "竖直分割： \n",
      "[array([[0, 1, 2, 3],\n",
      "       [4, 5, 6, 7]]), array([[ 8,  9, 10, 11],\n",
      "       [12, 13, 14, 15]])]\n"
     ]
    }
   ],
   "execution_count": 34
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T08:19:59.588660Z",
     "start_time": "2025-01-06T08:19:59.582339Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#12 数组中nan和inf\n",
    "'''\n",
    "C 语言中表示最大的正整数值是 0x7FFF FFFF， 最小的负整数是 0x8000 0000。\n",
    "查阅资料后， 发现 inf 表示无穷大， 需要使用 float(‘inf’ ) 函数来转化， 那么对应的就有\n",
    "float('-inf') 表示无穷小了。\n",
    "这样你就可以使用任意数来判断和它的关系了。\n",
    "那什么时候会出现 inf 呢？ 比如一个数字除以 0， Python 中会报错， 但是 numpy 中会是一个\n",
    "inf或者-inf\n",
    "另外还有 nan， 这种写法在 pandans 中常见， 表示缺失的数据， 所以一般用 nan 来表示。\n",
    "任何与其做运算结果都是 nan（比如零除以零， 在 numpy 中）\n",
    "'''\n",
    "# 创建一个 nan 和 inf #\n",
    "a = np.nan\n",
    "b = np.inf\n",
    "print(a, type(a))\n",
    "print(b, type(b))\n",
    "# --判断数组中为 nan 的个数\n",
    "t = np.arange(24, dtype=float).reshape(4, 6)\n",
    "# 将三行四列的数改成 nan\n",
    "t[3, 4] = np.nan\n",
    "# 可以使用 np.count_nonzero() 来判断非零的个数\n",
    "print(np.count_nonzero(t))\n",
    "# 并 且 np.nan != np.nan 结果 是 TRUE\n",
    "# 所以我们可以使用这两个结合使用判断 nan 的个数\n",
    "print(np.count_nonzero(t != t))\n",
    "# 将 nan 替换为 0\n",
    "t[np.isnan(t)] = 0\n",
    "print(t)"
   ],
   "id": "b64e1b41bda2c9dd",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "nan <class 'float'>\n",
      "inf <class 'float'>\n",
      "23\n",
      "1\n",
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8.  9. 10. 11.]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21.  0. 23.]]\n"
     ]
    }
   ],
   "execution_count": 35
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# ----------练习： 处理数组中 nan\n",
    "t = np.arange(24).reshape(4, 6).astype('float')\n",
    "# 将数组中的一部分替换 nan\n",
    "t[1, 3:] = np.nan\n",
    "print(t)\n",
    "print('-------------------')"
   ],
   "id": "cf8c0940b2d1877b"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
